<template>
  <div class="number">
    <div class="btn">
      <div class="btn-item" @click="addNum">随机添加</div>
      <div class="btn-item" @click="delNum">随机删除</div>
      <div class="btn-item" @click="sortNum">数字随机</div>
    </div>

    <transition-group name="nums" tag="ul">
      <li v-for="item in nums" :key="item">{{ item }}</li>
    </transition-group>
  </div>
</template>
<script>
import { ref, reactive } from 'vue'
export default {
  setup() {
    let nums = reactive([1, 2, 3, 4, 5, 6])
    let next = ref(7)
    // 数字随机
    const getRandomIndex = () => {
      return Math.floor(Math.random() * nums.length)
    }

    // 添加数字
    const addNum = () => {
      var index = getRandomIndex()
      nums.splice(index, 0, next.value)
      next.value++
    }

    // 字母随机
    const sortNum = () => {
      nums.sort(() => Math.random() - 0.5)
    }

    // 数字删除
    const delNum = () => {
      var index = getRandomIndex()
      nums.splice(index, 1)
    }

    // 添加数字
    return {
      nums,
      next,
      addNum,
      sortNum,
      delNum,
    }
  },
}
</script>
<style>
.number {
  width: 500px;
  border: 1px solid #ccc;
  margin: 0 auto;
}
.btn {
  display: flex;
  justify-content: space-between;
  background-color: #fff;
}

.btn .btn-item {
  width: 80px;
  height: 40px;
  display: flex;
  font-weight: bold;
  align-items: center;
  justify-content: center;
  border-radius: 5px;
  color: #fff;
  background-color: #f40;
  cursor: pointer;
}

/* 动画效果 */
.nums-enter {
  opacity: 0;
  transform: translateX(-100%);
}

.nums-enter-active,
.nums-leave-active,
.nums-move {
  transition: 0.5s;
}

.nums-leave-to {
  opacity: 0;
  transform: translateX(100%);
}

.nums-leave-active {
  position: absolute;
}
</style>
